home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1996 #3 / AmigaPlus_CD-ROM-EXTRA_Nr.3.bin / aminet-spiele / role on / larn / signal.c < prev    next >
C/C++ Source or Header  |  1997-12-31  |  6KB  |  184 lines

  1. #include <signal.h>
  2. #include "header.h"
  3. #include "larndefs.h"
  4.  
  5. #define BIT(a) (1<<((a)-1))
  6.  
  7. extern char savefilename[],wizard,predostuff,nosignal;
  8.  
  9. static s2choose()   /* text to be displayed if ^C during intro screen */
  10.     {
  11.     cursor(1,24); lprcat("Press "); setbold(); lprcat("return"); resetbold();
  12.     lprcat(" to continue: ");   lflush();
  13.     }
  14.  
  15. static void cntlc()    /* what to do for a ^C */
  16.     {
  17.     if (nosignal) return;   /* don't do anything if inhibited */
  18. #ifndef AMIGA
  19. # ifndef MSDOS
  20.     signal(SIGQUIT,SIG_IGN);
  21. # endif MSDOS
  22. #endif AMIGA
  23.     signal(SIGINT,SIG_IGN);
  24.     quit(); if (predostuff==1) s2choose(); else showplayer();
  25.     lflush();
  26. # ifndef MSDOS
  27. #ifndef AMIGA
  28.     signal(SIGQUIT,cntlc);
  29. #endif /* AMIGA */
  30. # endif
  31.     signal(SIGINT,cntlc);
  32.     }
  33.  
  34. #ifndef MSDOS
  35. /*
  36.  *  subroutine to save the game if a hangup signal
  37.  */
  38. static sgam()
  39.     {
  40.     savegame(savefilename);  wizard=1;  died(-257); /* hangup signal */
  41.     }
  42. #endif
  43.  
  44. #ifdef SIGTSTP
  45. static tstop() /* control Y */
  46.     {
  47.     if (nosignal)   return;  /* nothing if inhibited */
  48.     lcreat((char*)0);  clearvt100();    lflush();     signal(SIGTSTP,SIG_DFL);
  49. #ifdef SIGVTALRM
  50.     /* looks like BSD4.2 or higher - must clr mask for signal to take effect*/
  51.     sigsetmask(sigblock(0)& ~BIT(SIGTSTP));
  52. #endif
  53.     kill(getpid(),SIGTSTP);
  54.  
  55.     setupvt100();  signal(SIGTSTP,tstop);
  56.     if (predostuff==1) s2choose(); else drawscreen();
  57.     showplayer();   lflush();
  58.     }
  59. #endif SIGTSTP
  60.  
  61. /*
  62.  *  subroutine to issue the needed signal traps  called from main()
  63.  */
  64. static void sigfpe()  { sigpanic(SIGFPE); }
  65. # ifndef MSDOS
  66. #ifndef AMIGA
  67. static sigbus()  { sigpanic(SIGBUS); }
  68. static sigill()  { sigpanic(SIGILL); }   static sigtrap() { sigpanic(SIGTRAP); }
  69. static sigiot()  { sigpanic(SIGIOT); }   static sigemt()  { sigpanic(SIGEMT); }
  70. static sigsegv() { sigpanic(SIGSEGV); }  static sigsys()  { sigpanic(SIGSYS); }
  71. static sigpipe() { sigpanic(SIGPIPE); }  static sigterm() { sigpanic(SIGTERM); }
  72. #endif /* AMIGA */
  73. # endif /* MSDOS */
  74.  
  75. sigsetup()
  76.     {
  77.     signal(SIGINT,  cntlc);
  78.     signal(SIGFPE,  sigfpe);
  79. # ifndef MSDOS
  80. #ifndef AMIGA
  81.     signal(SIGBUS,  sigbus);        signal(SIGQUIT, cntlc);
  82.     signal(SIGKILL, SIG_IGN);       signal(SIGHUP,  sgam);
  83.     signal(SIGILL,  sigill);        signal(SIGTRAP, sigtrap);
  84.     signal(SIGIOT,  sigiot);        signal(SIGEMT,  sigemt);
  85.     signal(SIGSEGV, sigsegv);       signal(SIGSYS,  sigsys);
  86.     signal(SIGPIPE, sigpipe);       signal(SIGTERM, sigterm);
  87. #ifdef SIGTSTP
  88.     signal(SIGTSTP,tstop);      signal(SIGSTOP,tstop);
  89. #endif SIGTSTP
  90. #endif /* AMIGA */
  91. # endif
  92.     }
  93.  
  94. #ifdef MSDOS
  95. #define NSIG 9
  96. #endif
  97.  
  98. #ifdef VMS
  99. #define NSIG 16
  100. #endif
  101.  
  102. #ifdef BSD  /* for BSD UNIX? */
  103.  
  104. static char *signame[NSIG] = { "",
  105. "SIGHUP",  /*   1    hangup */
  106. "SIGINT",  /*   2    interrupt */
  107. "SIGQUIT", /*   3    quit */
  108. "SIGILL",  /*   4    illegal instruction (not reset when caught) */
  109. "SIGTRAP", /*   5    trace trap (not reset when caught) */
  110. "SIGIOT",  /*   6    IOT instruction */
  111. "SIGEMT",  /*   7    EMT instruction */
  112. "SIGFPE",  /*   8    floating point exception */
  113. "SIGKILL", /*   9    kill (cannot be caught or ignored) */
  114. "SIGBUS",  /*   10   bus error */
  115. "SIGSEGV", /*   11   segmentation violation */
  116. "SIGSYS",  /*   12   bad argument to system call */
  117. "SIGPIPE", /*   13   write on a pipe with no one to read it */
  118. "SIGALRM", /*   14   alarm clock */
  119. "SIGTERM", /*   15   software termination signal from kill */
  120. "SIGURG",  /*   16   urgent condition on IO channel */
  121. "SIGSTOP", /*   17   sendable stop signal not from tty */
  122. "SIGTSTP", /*   18   stop signal from tty */
  123. "SIGCONT", /*   19   continue a stopped process */
  124. "SIGCHLD", /*   20   to parent on child stop or exit */
  125. "SIGTTIN", /*   21   to readers pgrp upon background tty read */
  126. "SIGTTOU", /*   22   like TTIN for output if (tp->t_local<OSTOP) */
  127. "SIGIO",   /*   23   input/output possible signal */
  128. "SIGXCPU", /*   24   exceeded CPU time limit */
  129. "SIGXFSZ", /*   25   exceeded file size limit */
  130. "SIGVTALRM",/*  26   virtual time alarm */
  131. "SIGPROF", /*   27   profiling time alarm */
  132. "","","","" };
  133.  
  134. #else BSD   /* for system V? */
  135.  
  136. static char *signame[NSIG+1] = { "",
  137. "SIGHUP",  /*   1    hangup */
  138. "SIGINT",  /*   2    interrupt */
  139. "SIGQUIT", /*   3    quit */
  140. "SIGILL",  /*   4    illegal instruction (not reset when caught) */
  141. "SIGTRAP", /*   5    trace trap (not reset when caught) */
  142. "SIGIOT",  /*   6    IOT instruction */
  143. "SIGEMT",  /*   7    EMT instruction */
  144. # if MSDOS|AMIGA
  145. "SIGFPE"}; /*   8    floating point exception */
  146. # else MSDOS
  147. "SIGFPE",  /*   8    floating point exception */
  148. "SIGKILL", /*   9    kill (cannot be caught or ignored) */
  149. "SIGBUS",  /*   10   bus error */
  150. "SIGSEGV", /*   11   segmentation violation */
  151. "SIGSYS",  /*   12   bad argument to system call */
  152. "SIGPIPE", /*   13   write on a pipe with no one to read it */
  153. "SIGALRM", /*   14   alarm clock */
  154. # ifdef VMS
  155. "SIGTERM"}; /*  15   software termination signal from kill */
  156. # else VMS
  157. "SIGTERM", /*   15   software termination signal from kill */
  158. "SIGUSR1",  /*  16   user defines signal 1 */
  159. "SIGUSR2", /*   17   user defines signal 2 */
  160. "SIGCLD",  /*   18   child death */
  161. "SIGPWR" };  /*   19   power fail */
  162. # endif VMS
  163. # endif MSDOS
  164. # endif BSD
  165.  
  166. /*
  167.  *  routine to process a fatal error signal
  168.  */
  169. static sigpanic(sig)
  170. int sig;
  171. {
  172.     char buf[128];
  173.     signal(sig,SIG_DFL);
  174.     sprintf(buf,"\nLarn - Panic! Signal %d received [%s]",sig,signame[sig]);
  175.     write(2,buf,strlen(buf));  sleep(2);
  176.     sncbr();
  177.     savegame(savefilename); 
  178. # ifdef MSDOS
  179.     exit(1);
  180. # else
  181.     kill(getpid(),sig); /* this will terminate us */
  182. # endif
  183. }
  184.